﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace KingOfCSharp.iHunter.UI
{
    public partial class HtmlMessageViewer : UserControl, IMessageViewer
    {
        private Dictionary<string, string> customFields;

        /// <summary>
        /// Initializes a new instance of the <see cref="HtmlMessageViewer"/> class.
        /// </summary>
        public HtmlMessageViewer()
        {
            InitializeComponent();
            customFields = new Dictionary<string, string>();
        }

        private void HtmlMessageViewer_Load(object sender, EventArgs e)
        {
            this.TitleLabel.Font = new Font(this.Font.FontFamily, 15, FontStyle.Bold);
        }

        private void HtmlMessageViewer_FontChanged(object sender, EventArgs e)
        {
            this.TitleLabel.Font = new Font(this.Font.FontFamily, 15, FontStyle.Bold);
        }

        private void SetHeaderField(Label label, Control field, string content, bool display)
        {
            if (display)
            {
                label.Visible = true;
                field.Visible = true;
                field.Text = content;
            }
            else
            {
                label.Visible = false;
                field.Visible = false;
            }
        }


        #region IMessageViewer 成员

        public void DisplayMessage(IMessage message)
        {
            SuspendLayout();
            
            //Title
            this.TitleLabel.Text = message.Title;

            //fields
            SetHeaderField(AuthorLabel, AuthorTextBox, message.Author, !string.IsNullOrEmpty(message.Author));
            SetHeaderField(ReceiverLabel, ReceiverTextBox, message.Receiver, !string.IsNullOrEmpty(message.Receiver));
            SetHeaderField(CategoryLabel, CategoryTextBox, message.Category, !string.IsNullOrEmpty(message.Category));
            SetHeaderField(UpdateTimeLabel, UpdateTimeTextBox, message.UpdateTime.ToString(), message.UpdateTime >= message.PublishTime);
            SetHeaderField(PublishTimeLabel, PublishTimeTextBox, message.PublishTime.ToString(), message.PublishTime.Year > 1900);

            this.panel1.Height = tableLayoutPanel1.PreferredSize.Height;

            //Content
            this.HtmlBrowser.DocumentText = message.Content;
            ResumeLayout(true);
        }

        public System.Xml.XPath.XPathNavigator ContextualCommands
        {
            get { return null; }
        }

        public void ExecuteCommand(string name, System.Xml.XPath.XPathNavigator arg)
        {
            
        }

        #endregion

        #region IMessageViewer 成员


        public void ExcuteCommand(CommandUIEventArgs uiElement)
        {
            if (uiElement.Name == "Edit.Copy")
            {
                HtmlBrowser.Document.ExecCommand("Copy", false, null);
            }
        }

        public event CommandUIEventHandler UpdateCommandUI;

        #endregion
    }
}
